package chapter2;

/**
 * 打印两个有序链表的公共部分
 */
public class PrintCommonPart {
    public void printCommonNode(ListNode head1, ListNode head2) {
        StringBuilder s = new StringBuilder("");
        while (head1 != null && head2 != null) {
            if (head1.value < head2.value){
                head1 = head1.next;
            } else if (head1.value > head2.value) {
                head2 = head2.next;
            } else {
//                System.out.print(head1.value + " ");// 注意末尾有空格
                s = s.append(head1.value).append(" ");
                head1 = head1.next;
                head2 = head2.next;
            }
        }
        System.out.println(s.toString().trim());
    }
}
